package com.yunhe.sms.dao.activity.impl;

import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.sms.dao.activity.VoteDao;
import com.yunhe.sms.entity.activity.Vote;

/**
 * Created by Administrator on 2017/10/30 0030.
 */
public class VoteDaoImpl extends BaseDaoImpl<Vote> implements VoteDao{

    @Override
    public Long save(Vote vote) {

        String sql = " insert into activity_vote(title,banner,status,start_time,end_time,turnout,created_time,deleted) values(?,?,?,?,?,0,now(),0) ";
        Long id = super.insert(sql,vote.getTitle(),vote.getBanner(),vote.getStatus(),vote.getStartTime(),vote.getEndTime());
        return id;
    }

    @Override
    public void update(Vote vote) {
        String sql = " update activity_vote set title=?,banner=?,status=?,start_time=?,end_time=? where id=? ";
        super.cud(sql,vote.getTitle(),vote.getBanner(),vote.getStatus(),vote.getStartTime(),vote.getEndTime(),vote.getId());
    }

    @Override
    public void delete(Long id) {
        String sql = " update activity_vote set deleted = 1 where id = ? ";
        super.cud(sql,id);
    }

    @Override
    public Vote find(Long id) {
        String sql = " select id,title,banner,status,start_time startTime,end_time endTime,turnout from activity_vote where id = ? and deleted = 0 ";
        Vote vote = super.queryOne(sql, id);
        return vote;
    }

    @Override
    public Pageable<Vote> findPage(Integer offset, Integer limit) {
        /*String listSql = " select id,title,banner,status,start_time startTime,end_time endTime,turnout from activity_vote where deleted = 0 ";*/

        String listSql = " select id,title,banner,status,date_format(start_time,'%Y-%m-%d %H:%i:%s') start,date_format(end_time,'%Y-%m-%d %H:%i:%s') end,turnout from activity_vote where deleted = 0 ";

        String countSql = " select count(*) from activity_vote where deleted = 0 ";
        Pageable<Vote> votePageable = super.queryPage(listSql, countSql, offset, limit);
        return votePageable;
    }
}
